package com.itzixi.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.itzixi.common.pojo.JqGridResult;
import com.itzixi.common.utils.LeeJSONResult;
import com.itzixi.pojo.DataArticle;
import com.itzixi.service.DataArticleService;

/**
 * Created by zhangxh.apabi on 2017/9/22.
 */
@Controller
@RequestMapping("/dataArticle")
public class DataArticleController extends BaseController {

	final static Logger log = LoggerFactory.getLogger(DataArticleController.class);

	@Autowired
	private DataArticleService dataArticleService;

	/**
	 * 显示添加文章页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/createDataArticlePage")
	public ModelAndView showCreateDataArticle(HttpServletRequest request) {
		log.debug("显示添加文章页面");
		ModelAndView mv = new ModelAndView("dataArticle/createDataArticlePage");
		return mv;
	}

	/**
	 * 查询文章列表内容
	 * 
	 * @param article
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/getDataArticleList")
	@ResponseBody
	public JqGridResult getUserInfoList(DataArticleController article, Integer page, Integer pageSize) {
		if (page == null) {
			page = 1;
		}
		if (pageSize == null) {
			pageSize = 10;
		}
		JqGridResult result = dataArticleService.queryDataArticleList(null, page, pageSize);

		return result;
	}

	/**
	 * 修改或增加文章
	 * 
	 * @param dataArticle
	 * @return
	 */
	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	public LeeJSONResult saveOrUpdate(DataArticle dataArticle) {

		String articleId = dataArticle.getId();
		if (StringUtils.isEmpty(articleId)) {
			dataArticleService.saveDataArticle(dataArticle);
		} else {
			dataArticleService.updateDataArticleById(dataArticle);
		}
		return LeeJSONResult.ok();
	}

	/**
	 * 文章页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/dataArticleList")
	public String showdataArticleListPage(HttpServletRequest request) {
		return "dataArticle/dataArticleList";
	}

	/**
	 * 查询某一个文章
	 * 
	 * @param dataArticleId
	 * @param request
	 * @return
	 */
	@RequestMapping("/dataArticleDetail")
	public ModelAndView dataArticleDetail(String dataArticleId, HttpServletRequest request) {

		DataArticle dataArticle = dataArticleService.queryDataArticleById(dataArticleId);

		ModelAndView mv = new ModelAndView("dataArticle/dataArticleDetail");
		mv.addObject("dataArticle", dataArticle);
		return mv;
	}

	/**
	 * 删除文章
	 * 
	 * @param dataArticleId
	 * @return
	 */
	@RequestMapping("/deleteDataArticle")
	@ResponseBody
	public LeeJSONResult deleteItem(String dataArticleId) {

		if (StringUtils.isEmpty(dataArticleId)) {
			return LeeJSONResult.errorMsg("文章id为空或者不存在...");
		}
		dataArticleService.deleteDataArticleById(dataArticleId);
		return LeeJSONResult.ok();
	}

	/**
	 * 跳转到修改文章页面
	 * 
	 * @param userId
	 * @param request
	 * @return
	 */
	@RequestMapping("/modifyDataArticle")
	public ModelAndView modifyDataArticle(String dataArticleId, HttpServletRequest request) {

		DataArticle dataArticle = dataArticleService.queryDataArticleById(dataArticleId);

		ModelAndView mv = new ModelAndView("dataArticle/modifyDataArticle");
		mv.addObject("dataArticle", dataArticle);

		return mv;
	}
}
